import {_getZCondVars, COMMON_FUN, runExpr} from "./jexpr.js";
import {ZEachBase} from "./c_each.js";

class ZCond extends ZEachBase {
    constructor() {
        super();
        this._initChildNodes();
    }
    mounted() {
        requestAnimationFrame(() => {
            this.$ctx?.initShadow(this.shadowRoot);
            this.notify()
        })
    }
    unmounted() {
        this.$ctx.unRegister(this)
    }
    getConds() {
        let nodes = this._getCondNodes();
        let arr = nodes.map(v => v.getAttribute("cond"));
        let conds = new Set();
arr.forEach(v => {
    _getZCondVars(v, conds);
    this._log('webcom z-cond cond', v, conds);
})
        return [...conds]
    }

    /**
     *
     * @param node {HTMLTemplateElement}
     * @private
     */
    _createContent(node) {
        let doms = node.content.cloneNode(true);
        // console.dir(doms);
        let subApp = this.$ctx.createSubApp({})
        subApp.mount(doms);
        this._shadowRoot.appendChild(doms);
    }
    notify() {
        if (this._locks) {
            return
        }
        let nodes =  this._getCondNodes()
        this._log('webcom z-cond notify', nodes, this.$ctx);
        let isSomeTrue = false;
        if (Array.isArray(nodes)) {
            for (let i = 0; i < nodes.length; i++) {
                let node = nodes[i];
                if (node?.content && this.$ctx) {
                    let cond = node.getAttribute('cond');
                    let isTrue = runExpr(cond, {
                        ...this.$ctx.getData()
                    }, COMMON_FUN)
                    this._log('webcom z-cond notify', this.$ctx.getData(), cond, isTrue)

                    if (isTrue) {
                        if (!isSomeTrue) {
                            isSomeTrue = true;
                        }
                        this.setLock()
                        this.clear()
                        for (let i = 0, len =  1; i < len; ++i) {
                            // let doms = node.content.cloneNode(true);
                            // // console.dir(doms);
                            // let subApp = this.$ctx.createSubApp({})
                            // subApp.mount(doms);
                            // this._shadowRoot.appendChild(doms);
                            this._createContent(node)
                        }
                        requestAnimationFrame(() => {
                            this.$ctx.register(this)
                        })
                        break;
                    }
                }
            }
        }

        // 支持else
        if (!isSomeTrue) {
            let node = this.$defaultTpl;
            if (node) {
                this.setLock()
                this.clear()
                this._createContent(node)
                requestAnimationFrame(() => {
                    this.$ctx.register(this)
                })
            }
        }
    }
}

ZCond.defsel('z-cond',{
    attrs: {
        value: ''
    }
})